"""
KidsBuddy 1.0 API数据模型
定义API数据交换的模型和验证规则
"""

from typing import Dict, List, Optional, Any, Union
from dataclasses import dataclass
from datetime import datetime


@dataclass
class StudentProfile:
    """学生档案数据模型"""
    student_id: str
    name: str
    age: Optional[int] = None
    grade: Optional[str] = None
    created_at: datetime = datetime.now()
    settings: Dict[str, Any] = None


@dataclass
class SessionData:
    """会话数据模型"""
    session_id: str
    student_id: str
    start_time: datetime
    end_time: Optional[datetime] = None
    duration_seconds: Optional[float] = None
    status: str = "active"  # active, completed, interrupted
    metadata: Dict[str, Any] = None


@dataclass
class StudyStatistics:
    """学习统计数据模型"""
    session_id: str
    total_duration: float
    effective_duration: float
    posture_score_avg: float
    environment_score_avg: float
    interruption_count: int
    focus_periods: List[Dict[str, Any]]
    timestamp: datetime = datetime.now()


@dataclass
class AlertEvent:
    """警告事件数据模型"""
    alert_id: str
    session_id: str
    timestamp: datetime
    alert_type: str  # posture, environment, interference
    severity: str  # low, medium, high
    message: str
    data: Dict[str, Any] = None
    resolved: bool = False
    resolved_at: Optional[datetime] = None


@dataclass
class ApiResponse:
    """API响应数据模型"""
    success: bool
    message: str
    data: Optional[Any] = None
    errors: Optional[List[str]] = None
    timestamp: datetime = datetime.now()


def format_api_response(success: bool, message: str, data: Any = None, errors: List[str] = None) -> Dict[str, Any]:
    """
    格式化API响应
    
    Args:
        success: 是否成功
        message: 响应消息
        data: 响应数据
        errors: 错误列表
        
    Returns:
        格式化后的API响应字典
    """
    return {
        "success": success,
        "message": message,
        "data": data,
        "errors": errors,
        "timestamp": datetime.now().isoformat()
    }


def validate_session_id(session_id: str) -> bool:
    """
    验证会话ID格式
    
    Args:
        session_id: 会话ID
        
    Returns:
        是否为有效的会话ID
    """
    # 简单验证，可以扩展为更复杂的验证逻辑
    return session_id and session_id.startswith("session_")


def validate_student_id(student_id: str) -> bool:
    """
    验证学生ID格式
    
    Args:
        student_id: 学生ID
        
    Returns:
        是否为有效的学生ID
    """
    # 简单验证，可以扩展为更复杂的验证逻辑
    return student_id and len(student_id) >= 3